package cn.com.huzo.webapp.controller.master;

import cn.com.huzo.model.entities.master.ProductCatalogueEntity;
import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.master.ProductEntity;
import cn.com.huzo.service.master.ProductCatalogueManager;
import cn.com.huzo.service.master.ProductManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 产品信息控制器
 *
 * @author 吴旭
 */
@Controller
public class ProductController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">    
    @Resource(name = "ProductManager")
    private ProductManager _manager;

    @Resource(name = "ProductCatalogueManager")
    private ProductCatalogueManager _managerCatalogue;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到产品一览画面
     * <p>
     * @return
     */
    @RequestMapping("master/product/list.html")
    public ModelAndView redirectListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("master.product.list.def");
        return view;
    }

    /**
     * 产品编辑画面
     * <p>
     * @param session
     * @param action
     * @param productId
     * @param nodeId
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/product/{action}/{productId}/{nodeId}/edit.html",
            method = {RequestMethod.GET})
    public ModelAndView redirectEditWnd(HttpSession session, @PathVariable(value = "action") int action,
            @PathVariable(value = "productId") Long productId,
            @PathVariable(value = "nodeId") Long nodeId) throws Exception {
        ModelAndView view = new ModelAndView();
        view.addObject("action", action);
        view.addObject("productId", productId);

        if (nodeId != 0L) {
            ProductCatalogueEntity _item = _managerCatalogue.fetchByKey(nodeId);
            view.addObject("nodeId", nodeId);
            view.addObject("nodeText", _item.getParentName() + " → " + _item.getCatalogueName());
        }

        view.setViewName("master.product.edit.def");
        return view;
    }

    /**
     * 弹出产品选择画面
     * <p>
     * @return
     */
    @RequestMapping("master/product/choice.html")
    public ModelAndView redirectChoiceWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.master.product.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 根据标识取得产品数据。
     *
     * @param criterion 产品标识、当前用户、流程标识
     * @return 产品数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/product/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    ProductEntity fetchByKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.fetchByKey(MapUtils.getLong(criterion, "HeaderId"));
    }

    /**
     * 删除。
     *
     * @param criterion 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/product/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.delete(criterion);
    }

    /**
     * 根据条件取得产品信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 产品信息分页数据
     * @throws Exception
     */
    @RequestMapping(value = "master/product/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.fetchForPage(criterion);
    }

    /**
     * 保存产品信息数据。
     * <p>
     * @param instance 要保存的产品信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "master/product/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody ProductEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getProductId() > 0) { //编辑
            rowsAffected = (long) _manager.update(instance);
        } else {//新增
            rowsAffected = _manager.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 批量更新产品所属目录。
     *
     * @param itemList 要处理的数据列表
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/product/batchChangeCatalogue.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer batchChangeCatalogue(@RequestBody final Map<String, ? extends List<ProductEntity>> itemList) throws Exception {
        int rowsAffected = 0;
        if (itemList.get("updateList").size() > 0) {
            rowsAffected = _manager.batchChangeCatalogue(itemList.get("updateList"));
        }

        return rowsAffected;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据补全处理部分">
    /**
     * 根据输入的部分款号从产品信息中自动补全剩余的款号
     *
     * @param criterion 部分款号
     * @return 补全的款号列表
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/product/fetchStyleNoForAutoComplate.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<String> fetchStyleNoForAutoComplate(@RequestBody final Map<String, Object> criterion) throws Exception {
        if (criterion.get("keyword") != null) {
            return _manager.fetchStyleNoForAutoComplate(MapUtils.getString(criterion, "keyword"));
        } else {
            return new ArrayList();
        }
    }

    /**
     * 根据输入的部分系列号从产品信息中自动补全剩余的系列号
     *
     * @param criterion 部分系列号
     * @return 补全的款号列表
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/product/fetchSerialNoForAutoComplate.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<String> fetchSerialNoForAutoComplate(@RequestBody final Map<String, Object> criterion) throws Exception {
        if (criterion.get("keyword") != null) {
            return _manager.fetchSerialNoForAutoComplate(MapUtils.getString(criterion, "keyword"));
        } else {
            return new ArrayList();
        }
    }
    // </editor-fold>
}
